﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UIShare;
using EFong.View.Proxy;
using EFong.Contract;

namespace EFong.View.Report
{
    public partial class Frm_出单统计 : BasicForm
    {
        private Proxy.ReportServiceProxy _reportProxy { get { return Proxy.ReportServiceProxy.GetInstance(); } }
        private Proxy.BaseServiceProxy _baseProxy { get { return Proxy.BaseServiceProxy.GetInstance(); } }

        public Frm_出单统计()
        {
            InitializeComponent();
        }

        private void bar_Main_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            switch (e.Item.Name)
            {
                case "bbt_Export":
                    this.export();
                    break;
                case "bbt_Close":
                    this.Close();
                    break;
                default:
                    break;
            }
        }

        protected override void InitFields()
        {
        }

        protected override void LoadDatas()
        {
            this.cmb_User.Properties.BindingData(_baseProxy.GetEffUser().ReturnValue);
            this.dte_Month.DateTime = DateTime.Now;
        }

        protected override void Events()
        {
            this.btn_Search.Click += (s, e) =>
            {
                List<每日订单报表> list = null;
                User user = cmb_User.GetValue() as User;
                if (chkOrderUser.Checked)
                {
                    list = _reportProxy.GetReport16(user, this.dte_Month.DateTime).ReturnValue;
                }
                else
                {
                    list = _reportProxy.GetReport17(user, this.dte_Month.DateTime).ReturnValue;
                }

                bool sourceIsNull = list == null;
                bool sourceIsEmpty = sourceIsNull || list.Count == 0;

                if (sourceIsEmpty)
                {
                    MsgBox.Info("没有找到符合条件的数据.");
                    this.grd_Flight.DataSource = new List<每日订单报表>();
                    this.grdTrain.DataSource = new List<每日订单报表>();
                    this.grdHotel.DataSource = new List<每日订单报表>();
                    this.grdOther.DataSource = new List<每日订单报表>();
                    this.grdTourParty.DataSource = new List<每日订单报表>();

                    return;
                }

                //this.grd_Report.DataSource = list;
                this.grd_Flight.DataSource = list.Where(o => o.单据类型.StartsWith("飞机票")).ToList();
                this.grdTrain.DataSource = list.Where(o => o.单据类型.StartsWith("火车票")).ToList();
                this.grdHotel.DataSource = list.Where(o => o.单据类型.StartsWith("酒店")).ToList();
                this.grdOther.DataSource = list.Where(o => o.单据类型.StartsWith("其他")).ToList();
                this.grdTourParty.DataSource = list.Where(o => o.单据类型.StartsWith("自组团")).ToList();



                this.txtTotalPrice.Text = (from c in list
                                           select c.价格).Sum().ToString();
                this.txtTotalProfit.Text = (from c in list
                                            select c.利润).Sum().ToString();
            };

            this.chkOrderUser.Click += (s, e) =>
            {
                //chkOrderUser.Checked = !chkOrderUser.Checked;
                chkProviderUser.Checked = chkOrderUser.Checked;
            };
            this.chkProviderUser.Click += (s, e) =>
            {
                //chkProviderUser.Checked = !chkProviderUser.Checked;
                chkOrderUser.Checked = chkProviderUser.Checked;
            };
        }

        private void export()
        {
            switch (tcReport.SelectedTabPageIndex)
            {
                case 0:
                    FileExport.ExportToExcel(this.grd_Flight, "出单统计");
                    break;
                case 1:
                    FileExport.ExportToExcel(this.grdTrain, "出单统计");
                    break;
                case 2:
                    FileExport.ExportToExcel(this.grdHotel, "出单统计");
                    break;
                case 3:
                    FileExport.ExportToExcel(this.grdOther, "出单统计");
                    break;
                case 4:
                    FileExport.ExportToExcel(this.grdTourParty, "出单统计");
                    break;
                default:
                    break;
            }


        }
    }
}
